package com.yida.gwj.report.service.impl;

import com.yida.common.core.utils.DateUtils;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.report.domain.dto.GwjReportDepositBusinessDTO;
import com.yida.gwj.report.domain.request.GwjReportDepositBusinessQueryRequest;
import com.yida.gwj.report.mapper.GwjReportDepositBusinessMapper;
import com.yida.gwj.report.service.IGwjReportDepositBusinessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 缴存业务统计报表Service业务层处理
 *
 * @author lqq
 * @date 2023-03-06
 */
@Service
public class GwjReportDepositBusinessServiceImpl implements IGwjReportDepositBusinessService {

	@Resource
	private GwjReportDepositBusinessMapper gwjReportDepositBusinessMapper;

	/**
	 * 统计缴存业务金额报表
	 *
	 * @param request 请求参数
	 * @return /
	 */
	@Override
	public List<GwjReportDepositBusinessDTO> statisticsAllDepositBusiness(GwjReportDepositBusinessQueryRequest request) {
		List<GwjReportDepositBusinessDTO> gwjReportDepositBusinessDTOS = gwjReportDepositBusinessMapper.statisticsAllDepositBusiness(request);
		//设置缴存时间和缴存主体
		gwjReportDepositBusinessDTOS.forEach(b -> {
			b.setDepositTime(DateUtils.dateTime(request.getStartTime()) + "至" + DateUtils.dateTime(request.getEndTime()));
			b.setBizFlagStr(GwjBusinessFlagType.getInfoByCode(b.getBizFlagId()));
		});
		//合计
		gwjReportDepositBusinessDTOS.add(new GwjReportDepositBusinessDTO() {{
			setBizFlagStr("合计");
			setDepositTime(DateUtils.dateTime(request.getStartTime()) + "至" + DateUtils.dateTime(request.getEndTime()));
			setTotalMoney(gwjReportDepositBusinessDTOS.stream().map(GwjReportDepositBusinessDTO::getTotalMoney).reduce(BigDecimal.ZERO, BigDecimal::add));
			setTotalFactMoney(gwjReportDepositBusinessDTOS.stream().map(GwjReportDepositBusinessDTO::getTotalFactMoney).reduce(BigDecimal.ZERO, BigDecimal::add));
			setTotalSize(gwjReportDepositBusinessDTOS.stream().map(GwjReportDepositBusinessDTO::getTotalSize).reduce(BigDecimal.ZERO, BigDecimal::add));
			setTotalCommunity(gwjReportDepositBusinessDTOS.stream().map(GwjReportDepositBusinessDTO::getTotalCommunity).reduce(0L, Long::sum));
			setTotalBuild(gwjReportDepositBusinessDTOS.stream().map(GwjReportDepositBusinessDTO::getTotalBuild).reduce(0L, Long::sum));
			setTotalOwner(gwjReportDepositBusinessDTOS.stream().map(GwjReportDepositBusinessDTO::getTotalOwner).reduce(0L, Long::sum));
			setCommunityName(gwjReportDepositBusinessDTOS.stream().map(GwjReportDepositBusinessDTO::getCommunityName).collect(Collectors.joining(",")));
		}});
		return gwjReportDepositBusinessDTOS;
	}

	/**
	 * 统计单个缴存业务金额报表
	 *
	 * @param request 请求参数
	 * @return /
	 */
	@Override
	public List<GwjReportDepositBusinessDTO> statisticsOneDepositBusiness(GwjReportDepositBusinessQueryRequest request) {
		List<GwjReportDepositBusinessDTO> gwjReportDepositBusinessDTOS = gwjReportDepositBusinessMapper.statisticsOneDepositBusiness(request);
		gwjReportDepositBusinessDTOS.forEach(gwjReportDepositBusinessDTO -> {
			gwjReportDepositBusinessDTO.setDepositTime(DateUtils.dateTime(request.getStartTime()) + "至" + DateUtils.dateTime(request.getEndTime()));
			gwjReportDepositBusinessDTO.setTotalCommunity(1L);
		});
		return gwjReportDepositBusinessDTOS;
	}
}
